﻿namespace HutongGames.PlayMakerEditor
{
    using System;
    using UnityEditor;
    using UnityEngine;

    internal class WatermarkSelector
    {
        private static float gridHeight;
        private static float gridWidth;
        private const int numColumns = 4;
        private static Vector2 scrollViewPosition;
        private static int selectedWatermarkIndex;
        private static GUIContent[] watermarkThumbs;

        private static void Cancel()
        {
            FsmEditor.Inspector.SetMode(HutongGames.PlayMakerEditor.InspectorMode.FsmInspector);
        }

        public static void Init()
        {
            Texture[] watermarkTextures = FsmEditorUtility.GetWatermarkTextures(true);
            string[] watermarkNames = FsmEditorUtility.GetWatermarkNames();
            watermarkThumbs = new GUIContent[watermarkTextures.Length];
            for (int i = 0; i < watermarkTextures.Length; i++)
            {
                watermarkThumbs[i] = new GUIContent(watermarkTextures[i], watermarkNames[i]);
            }
            gridWidth = 326f;
            gridHeight = ((watermarkTextures.Length / 4) * gridWidth) / 4f;
        }

        public static void OnGUI()
        {
            GUILayout.Label("Select A Watermark:", new GUILayoutOption[0]);
            FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
            scrollViewPosition = GUILayout.BeginScrollView(scrollViewPosition, new GUILayoutOption[0]);
            Color color = GUI.color;
            GUI.contentColor = FsmEditorStyles.WatermarkTintSolid;
            int index = GUILayout.SelectionGrid(selectedWatermarkIndex, watermarkThumbs, 4, new GUILayoutOption[] { GUILayout.Width(gridWidth), GUILayout.Height(gridHeight) });
            if (index != selectedWatermarkIndex)
            {
                SelectWatermark(index);
            }
            GUI.contentColor = color;
            GUILayout.EndScrollView();
            FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
            if (GUILayout.Button("Clear Watermark", new GUILayoutOption[0]))
            {
                FsmEditorUtility.SetWatermarkTexture(FsmEditor.SelectedFsmComponent, "");
                Cancel();
            }
            if (GUILayout.Button("Finished", new GUILayoutOption[0]))
            {
                Cancel();
            }
            if (FsmEditorSettings.ShowHints)
            {
                GUILayout.Box("Watermarks can help you quickly identify FSMs.\nPut custom watermark images in: PlayMaker/Editor/Watermarks.\nUse the existing watermarks as a guide.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
            }
            EditorGUILayout.Space();
        }

        public static void ResetSelection()
        {
            if (watermarkThumbs == null)
            {
                Init();
            }
            Texture watermarkTexture = FsmEditorUtility.GetWatermarkTexture(FsmEditor.SelectedFsmComponent);
            selectedWatermarkIndex = -1;
            for (int i = 0; i < watermarkThumbs.Length; i++)
            {
                if (watermarkTexture == watermarkThumbs[i].image)
                {
                    selectedWatermarkIndex = i;
                }
            }
        }

        private static void SelectWatermark(int index)
        {
            string[] watermarkNames = FsmEditorUtility.GetWatermarkNames();
            FsmEditorUtility.SetWatermarkTexture(FsmEditor.SelectedFsmComponent, watermarkNames[index]);
            FsmEditor.SetFsmDirty(false, false);
            selectedWatermarkIndex = index;
        }
    }
}

